//
//  SubordIndustryVC.m
//  NtoccShipper
//
//  Created by 朱崇瑾 on 2020/7/31.
//  Copyright © 2020 zhuchongjin. All rights reserved.
//

#import "SubordIndustryVC.h"
#import "SubordIndustryCell.h"

@interface SubordIndustryVC ()<UITableViewDelegate ,UITableViewDataSource>
@property (strong, nonatomic) CJTableView *tableViewFisrt;
@property (strong, nonatomic) CJTableView *tableViewSecond;
@property (strong, nonatomic) CJTableView *tableViewThree;
@property (strong, nonatomic) CJTableView *tableViewFoure;
@property (nonatomic,strong)NSMutableArray *arrDataFirst;
@property (nonatomic,strong)NSMutableArray *arrDataSecond;
@property (nonatomic,strong)NSMutableArray *arrDataThree;
@property (nonatomic,strong)NSMutableArray *arrDataFoure;
@property (nonatomic,strong) SubordIndustryCell *subordIndustryCell;
//private String industryCode; //货物所属行业code
//private String industryName;//货物所属行业名称
//private String industryList;//货物所属行业
@property (nonatomic,copy) NSString *strIndustryCode;
@property (nonatomic,copy) NSString *strIndustryName;
@property (nonatomic,copy) NSString *strindustryList;

@end

@implementation SubordIndustryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:@"货物所属行业"];
    [self setBackLeftButton:@""];
    self.subordIndustryCell = [[SubordIndustryCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self.view addSubview:self.tableViewFisrt];
    [self.view addSubview:self.tableViewSecond];
    [self.view addSubview:self.tableViewThree];
    [self.view addSubview:self.tableViewFoure];

    [self setRightImage:CJLString(@"保存") image:@""];

    self.strIndustryCode = @"";
    self.strindustryList= @"";
    self.strIndustryName = @"";
//    WS(ws);
//    self.tableViewFisrt.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableViewFisrt.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
//           [ws.tableView.mj_header beginRefreshing];
//    }];
    [self initData];
    [self loadDataWithParentCode:@"" level:@"1"];
}

- (void)RightTextClick:(UIButton *)sender{
   
//    DLog(@"----%@  ===== %@  -=-=-= %@",self.strIndustryCode ,self.strIndustryName ,self.strindustryList);
    if ([self.strIndustryCode isEqualToString:@""]) {
        [CJAlert showStringDismissWithDelay:@"请选择货物所属行业"];
    }else{
        NSDictionary *dic = @{
            @"industryCode":self.strIndustryCode,
            @"industryName":self.strIndustryName,
            @"industryList":self.strindustryList
        };
        
        if (self.blockSuccess) {
            self.blockSuccess(dic);
        }
        [self pop];
    }

}

- (void)initData{
    
    self.arrDataFirst = [[NSMutableArray alloc]init];
    self.arrDataSecond = [[NSMutableArray alloc]init];
    self.arrDataThree = [[NSMutableArray alloc]init];
    self.arrDataFoure = [[NSMutableArray alloc]init];
}

///  1234 四层
/// @param strCode <#stCode description#>
/// @param strLevel <#strLevel description#>
- (void)loadDataWithParentCode:(NSString *)strCode level:(NSString *)strLevel{

    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/industry",CJBaseUrl);
    NSDictionary *dicPra = @{
                             @"level":strLevel, // 1234 四层
                             @"parentCode":strCode
    };
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {

        DLog(@"++++++++%@",responseObject);
        NSArray *list = responseObject[@"records"];
        NSError * error ;
        NSArray *arr = [SubordIndustryMoel arrayOfModelsFromDictionaries:list error:&error];
            
        if ([strLevel isEqualToString:@"1"]) {
            
            [ws.arrDataFirst removeAllObjects];
            [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                SubordIndustryMoel *model = arr[idx];
                model.selected = NO;
                [ws.arrDataFirst addObject:model];
            }];
//            [ws.arrDataFirst addObjectsFromArray:arr];
            [ws.tableViewFisrt reloadData];
            
        }else if ([strLevel isEqualToString:@"2"]){
            
            [ws.arrDataSecond removeAllObjects];
//            [ws.arrDataSecond addObjectsFromArray:arr];
            [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                SubordIndustryMoel *model = arr[idx];
                model.selected = NO;
                [ws.arrDataSecond addObject:model];
            }];
            [ws.tableViewSecond reloadData];
            
        }else if ([strLevel isEqualToString:@"3"]){
            
            [ws.arrDataThree removeAllObjects];
//            [ws.arrDataThree addObjectsFromArray:arr];
            [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                SubordIndustryMoel *model = arr[idx];
                model.selected = NO;
                [ws.arrDataThree addObject:model];
            }];
            [ws.tableViewThree reloadData];
            
        }else if ([strLevel isEqualToString:@"4"]){
            
            [ws.arrDataFoure removeAllObjects];
//            [ws.arrDataFoure addObjectsFromArray:arr];
            [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                SubordIndustryMoel *model = arr[idx];
                model.selected = NO;
                [ws.arrDataFoure addObject:model];
            }];
            [ws.tableViewFoure reloadData];
        }
        
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
//        [self.tableViewFisrt.mj_header endRefreshing];
//        [self.tableViewFisrt.mj_footer endRefreshingWithNoMoreData];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == self.tableViewFisrt) {
        return self.arrDataFirst.count;

    }else if (tableView == self.tableViewSecond) {
        return self.arrDataSecond.count;

    }else if (tableView == self.tableViewThree) {
        return self.arrDataThree.count;

    }else{
        return self.arrDataFoure.count;
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == self.tableViewFisrt) {
        
        static NSString *identifier = @"SubordIndustryCell";
        SubordIndustryCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[SubordIndustryCell alloc] initWithFlex:nil reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        SubordIndustryMoel *model = self.arrDataFirst[indexPath.row];
        [cell setCellInfoWithModel:model];
        return cell;
        
    }else if (tableView == self.tableViewSecond){
        
        static NSString *identifier = @"SubordIndustryCell2";
        SubordIndustryCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[SubordIndustryCell alloc] initWithFlex:nil reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        SubordIndustryMoel *model = self.arrDataSecond[indexPath.row];
        [cell setCellInfoWithModel:model];
        return cell;
        
    }else if (tableView == self.tableViewThree){
        
        static NSString *identifier = @"SubordIndustryCell3";
        SubordIndustryCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[SubordIndustryCell alloc] initWithFlex:nil reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        SubordIndustryMoel *model = self.arrDataThree[indexPath.row];
        [cell setCellInfoWithModel:model];
        return cell;
        
    }else{
        
        static NSString *identifier = @"SubordIndustryCell4";
        SubordIndustryCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[SubordIndustryCell alloc] initWithFlex:nil reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        SubordIndustryMoel *model = self.arrDataFoure[indexPath.row];
        [cell setCellInfoWithModel:model];
        return cell;
    }
//    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  //  点击第一列 开始加载第二列  清除 3  4 列 一次类推
    DLog(@"-------");
    
    if (tableView == self.tableViewFisrt) {
        SubordIndustryMoel *model = self.arrDataFirst[indexPath.row];
        //
//        model.selected = YES;
        self.strIndustryCode = model.industryCode;
        self.strIndustryName = model.industryName;
        self.strindustryList = @"";
        self.strindustryList = model.industryCode;

        [self.arrDataFirst enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            SubordIndustryMoel *mod = self.arrDataFirst[idx];
            if (idx == indexPath.row) {
                mod.selected = YES;
            }else{
                mod.selected = NO;
            }

        }];
        [self.tableViewFisrt reloadData];
        
        [self.arrDataSecond removeAllObjects];
        [self.tableViewSecond reloadData];
        [self.arrDataThree removeAllObjects];
        [self.tableViewThree reloadData];
        [self.arrDataFoure removeAllObjects];
        [self.tableViewFoure reloadData];
        
        [self loadDataWithParentCode:model.industryCode level:@"2"];
        
    }else if (tableView == self.tableViewSecond){
        
        SubordIndustryMoel *model = self.arrDataSecond[indexPath.row];
        self.strIndustryCode = model.industryCode;
        self.strIndustryName = model.industryName;
//        DLog(@"==== %@",self.strindustryList);
        NSArray *arrlistCode = [CJControl componentsSeparatedByString:self.strindustryList withSign:@","];
        if (arrlistCode.count >= 2) {
            self.strindustryList = @"";
             self.strindustryList = CJStringWithFormat(@"%@,%@",arrlistCode[0],model.industryCode);
        }else{
            self.strindustryList = CJStringWithFormat(@"%@,%@",self.strindustryList,model.industryCode);
        }
        
        [self.arrDataSecond enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            SubordIndustryMoel *mod = self.arrDataSecond[idx];
            if (idx == indexPath.row) {
                mod.selected = YES;
            }else{
                mod.selected = NO;
            }
        }];
        
        [self.tableViewSecond reloadData];
        
        [self.arrDataThree removeAllObjects];
        [self.tableViewThree reloadData];
        [self.arrDataFoure removeAllObjects];
        [self.tableViewFoure reloadData];
        
        [self loadDataWithParentCode:model.industryCode level:@"3"];
        
    }else if (tableView == self.tableViewThree){
        
        SubordIndustryMoel *model = self.arrDataThree[indexPath.row];
        self.strIndustryCode = model.industryCode;
        self.strIndustryName = model.industryName;
        
        NSArray *arrlistCode = [CJControl componentsSeparatedByString:self.strindustryList withSign:@","];
        if (arrlistCode.count >= 3) {
            self.strindustryList = @"";
             self.strindustryList = CJStringWithFormat(@"%@,%@,%@",arrlistCode[0],arrlistCode[1],model.industryCode);
        }else{
            self.strindustryList = CJStringWithFormat(@"%@,%@",self.strindustryList,model.industryCode);
        }
        
        [self.tableViewThree reloadData];
        
        [self.arrDataFoure removeAllObjects];
        [self.tableViewFoure reloadData];
        
        [self loadDataWithParentCode:model.industryCode level:@"4"];

        [self.arrDataThree enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            SubordIndustryMoel *mod = self.arrDataThree[idx];
            if (idx == indexPath.row) {
                mod.selected = YES;
            }else{
                mod.selected = NO;
            }

        }];

    }else{
        
        SubordIndustryMoel *model = self.arrDataFoure[indexPath.row];
//        DLog(@"点击最一列了");
        self.strIndustryCode = model.industryCode;
        self.strIndustryName = model.industryName;
    
        NSArray *arrlistCode = [CJControl componentsSeparatedByString:self.strindustryList withSign:@","];
        if (arrlistCode.count >= 4) {
            self.strindustryList = @"";
            self.strindustryList = CJStringWithFormat(@"%@,%@,%@,%@",arrlistCode[0],arrlistCode[1],arrlistCode[2],model.industryCode);
        }else{
            self.strindustryList = CJStringWithFormat(@"%@,%@",self.strindustryList,model.industryCode);
        }
        
        [self.arrDataFoure enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            SubordIndustryMoel *mod = self.arrDataFoure[idx];
            if (idx == indexPath.row) {
                mod.selected = YES;
            }else{
                mod.selected = NO;
            }
        }];
        [self.tableViewFoure reloadData];
    }
}

- (CJTableView *)tableViewFisrt{
    if (!_tableViewFisrt) {
        _tableViewFisrt = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight + CJTableToTopHeight , SCREEN_WIDTH/4, SCREEN_HEIGHT-kTopHeight - CJTableToTopHeight ),UITableViewStylePlain);
        
        _tableViewFisrt.tbDelegate(self)
            .tbDataSource(self)
            .tbSetOther(YES)
            .tbEstimatedRowHeight(YES)
        //        .tbMjHeadFooter(YES)
        ;
    }
    return _tableViewFisrt;
}

- (CJTableView *)tableViewSecond{
    if (!_tableViewSecond) {
        _tableViewSecond = CJTableView.tbInitFrameStyle(CGRectMake(_tableViewFisrt.right, _tableViewFisrt.top, _tableViewFisrt.width, _tableViewFisrt.height),UITableViewStylePlain);
        _tableViewSecond.tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
//        .tbMjHeadFooter(YES)
        ;
    }
    return _tableViewSecond;
}

- (CJTableView *)tableViewThree{
    if (!_tableViewThree) {
        _tableViewThree = CJTableView.tbInitFrameStyle(CGRectMake(_tableViewSecond.right, _tableViewFisrt.top , _tableViewFisrt.width, _tableViewFisrt.height),UITableViewStylePlain);
        _tableViewThree.tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
//        .tbMjHeadFooter(YES)
        ;
    }
    return _tableViewThree;
}

- (CJTableView *)tableViewFoure{
    if (!_tableViewFoure) {
        _tableViewFoure = CJTableView.tbInitFrameStyle(CGRectMake(_tableViewThree.right ,_tableViewFisrt.top, _tableViewFisrt.width, _tableViewFisrt.height),UITableViewStylePlain);
        _tableViewFoure.tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
//        .tbMjHeadFooter(YES)
        ;
    }
    return _tableViewFoure;
}







@end


